C#
// 檔案路徑
string[] filePaths = new string[] { @"./myscores.csv" };
foreach (string file in filePaths)
{
// 讀取檔案
StreamReader sr = new StreamReader(file);
// 讀取CSV第一列
string[] Keys = sr.ReadLine().Split(',');
Dictionary<string, List<string>> data = new Dictionary<string, List<string>>();
// 取出參數
while (!sr.EndOfStream)
{
// 讀取Row
string[] row = sr.ReadLine().Split(',');
for (int i = 0; i < Keys.Length; i++)
{
string key = Keys[i];
// 有key時,進行新增value
if (data.Keys.Contains(key))
{
data[key].Add(row[i]);
}
else // 沒key時,實例List並放value
{
data.Add(key, new List<string>() { row[i] });
}
}
}
// 轉換成json
var json = JsonConvert.SerializeObject(data);
Console.WriteLine(json);
// 關閉
sr.Close();
}